home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SNNSV32.ZIP / SNNSv3.2 / xgui / sources / d3_graph.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-25  |  10.2 KB  |  456 lines

  1. /*****************************************************************************
  2.   FILE           : d3_graph.c
  3.   SHORTNAME      : graph.c
  4.   SNNS VERSION   : 3.2
  5.  
  6.   PURPOSE        : routines for polygons and colors
  7.   NOTES          :
  8.  
  9.   AUTHOR         : Ralf Huebner
  10.   DATE           : 1.12.1991
  11.  
  12.   CHANGED BY     : Sven Doering
  13.   IDENTIFICATION : @(#)d3_graph.c    1.14 3/2/94
  14.   SCCS VERSION   : 1.14
  15.   LAST CHANGE    : 3/2/94
  16.  
  17.              Copyright (c) 1990-1994  SNNS Group, IPVR, Univ. Stuttgart, FRG
  18.  
  19. ******************************************************************************/
  20.  
  21. #include <stdio.h>
  22. #include <values.h>
  23. #include <math.h>
  24.  
  25. #include <X11/Xlib.h>
  26. #include <X11/X.h>
  27. #include <X11/Xutil.h>
  28. #include <X11/Xos.h>
  29. #include <X11/Intrinsic.h>
  30.  
  31. #include "ui.h"
  32.  
  33. #include "ui_mainP.h"
  34. #include "ui_event.h"
  35. #include "ui_color.h"
  36. #include "d3_global.h"
  37. #include "d3_zgraph.h"
  38. #include "d3_point.h"
  39.  
  40. #include "d3_graph.ph"
  41.  
  42.  
  43.  
  44.  
  45. /*****************************************************************************
  46.   FUNCTION : incrementalize_y
  47.  
  48.   PURPOSE  : increment y
  49.   RETURNS  : 
  50.   NOTES    :
  51.  
  52. ******************************************************************************/
  53.  
  54.  
  55. static void incrementalize_y (register float *p1, register float *p2, register float *p, register float *dp, int y, register int mask)
  56. {
  57.     float dy, frac;
  58.  
  59.     dy = *(*(vector *)p2+1) - *(*(vector *)p1+1);
  60.     if (dy==0.0) dy = 1.0;
  61.     frac = y + 0.5 - *(*(vector *)p1+1);
  62.  
  63.     for (; mask!=0; mask>>=1, p1++, p2++, p++, dp++)
  64.         if (mask&1) {
  65.             *dp = (*p2-*p1) / dy;
  66.             *p = *p1 + *dp * frac;
  67.         }
  68. }
  69.  
  70.  
  71.  
  72. /*****************************************************************************
  73.   FUNCTION : incrementalize_x
  74.  
  75.   PURPOSE  : increment x
  76.   RETURNS  : 
  77.   NOTES    :
  78.  
  79. ******************************************************************************/
  80.  
  81.  
  82.  
  83. static void incrementalize_x (register float *p1, register float *p2, register float *p, register float *dp, int x, register int mask)
  84. {
  85.     float dx, frac;
  86.  
  87.     dx = **(vector *)p2 - **(vector *)p1;
  88.     if (dx==0.0) dx = 1.0;
  89.     frac = x + 0.5 - **(vector *)p1;
  90.  
  91.     for (; mask!=0; mask>>=1, p1++, p2++, p++, dp++)
  92.         if (mask&1) {
  93.             *dp = (*p2-*p1) / dx;
  94.             *p = *p1 + *dp * frac;
  95.         }
  96. }
  97.  
  98.  
  99.  
  100. /*****************************************************************************
  101.   FUNCTION : increment
  102.  
  103.   PURPOSE  : increment a masked value
  104.   RETURNS  : 
  105.   NOTES    :
  106.  
  107. ******************************************************************************/
  108.  
  109.  
  110. static void increment (register float *p, register float *dp, register int mask)
  111. {
  112.     for (; mask!=0; mask>>=1, p++, dp++)
  113.         if (mask&1)
  114.             *p += *dp;
  115. }
  116.  
  117.  
  118.  
  119.  
  120. /*****************************************************************************
  121.   FUNCTION : put_pixel
  122.  
  123.   PURPOSE  : draw a z-buffered pixel
  124.   RETURNS  : 
  125.   NOTES    :
  126.  
  127. ******************************************************************************/
  128.  
  129.  
  130.  
  131. static void put_pixel (int px, int py, vector point)
  132. {
  133.     float zp;
  134.  
  135.     d3_readZbuffer (px, py, &zp);
  136.     if (point[2] < zp) {
  137.         d3_putColPixel (px, py);
  138.         d3_writeZbuffer (px, py, point[2]);
  139.     }
  140. }
  141.  
  142.  
  143.  
  144.  
  145. /*****************************************************************************
  146.   FUNCTION : scanline
  147.  
  148.   PURPOSE  : scan a horizontal line in a polygon
  149.   RETURNS  : 
  150.   NOTES    :
  151.  
  152. ******************************************************************************/
  153.  
  154.  
  155.  
  156. static void scanline (int y, vector l, vector r, int mask)
  157. {
  158.     int x, lx, rx;
  159.     vector p, dp;
  160.  
  161.     mask &= ~POLY_MASK(0);
  162.     lx = ceil ((*l) - 0.5);
  163.     if (lx<d3_clipWindow.x0) lx = d3_clipWindow.x0;
  164.     rx = floor ((*r) - 0.5);
  165.     if (rx>d3_clipWindow.x1) rx = d3_clipWindow.x1;
  166.     if (lx>rx) return;
  167.     incrementalize_x (l, r, p, dp, lx, mask);
  168.     for (x=lx; x<=rx; x++) {
  169.         put_pixel (x, y, p);
  170.         increment (p, dp, mask);
  171.     }
  172. }
  173.  
  174.  
  175.  
  176.  
  177. /*****************************************************************************
  178.   FUNCTION : d3_drawPoly
  179.  
  180.   PURPOSE  : draw a zbuffered polygon
  181.   RETURNS  : 
  182.   NOTES    :
  183.  
  184. ******************************************************************************/
  185.  
  186.  
  187.  
  188. void d3_drawPoly (register d3_polygon_type *p)
  189. {
  190.     register int i, li, ri, y, ly, ry, top, rem, mask;
  191.     float ymin;
  192.     vector l, r, dl, dr;
  193.  
  194.     p->mask = POLY_MASK(0) | POLY_MASK(1) | POLY_MASK(2);
  195.  
  196.     ymin = MAXFLOAT;
  197.     for (i=0; i<p->n; i++) {            /* find top vertex */
  198.         if (p->vert[i][1] < ymin) {
  199.             ymin = (p->vert[i])[1];
  200.             top = i;
  201.         }
  202.     }
  203.     li = ri = top;                     /* left and right vertex indices */
  204.     rem = p->n;                        /* number of vertices remaining */
  205.     y = ceil (ymin - 0.5);             /* current scanline */
  206.     ly = ry = y-1;                     /* lower end of left & right edges */
  207.     mask = p->mask & ~POLY_MASK(1);   /* stop interpolating screen y */
  208.  
  209.     while (rem > 0) {
  210.          while (ly<=y && rem>0) {
  211.              rem--;
  212.              i = li-1;
  213.              if (i<0) i = p->n-1;
  214.              incrementalize_y (p->vert[li], p->vert[i], l, dl, y, mask);
  215.              ly = floor (p->vert[i][1] + 0.5);
  216.              li = i;
  217.          }
  218.          while (ry<=y && rem>0) {
  219.              rem--;
  220.              i = ri+1;
  221.              if (i>=p->n) i = 0;
  222.              incrementalize_y (p->vert[ri], p->vert[i], r, dr, y, mask);
  223.              ry = floor (p->vert[i][1] + 0.5);
  224.              ri = i;
  225.          }
  226.  
  227.  
  228.          while (y<ly && y<ry) {
  229.              if (y>=d3_clipWindow.y0 && y<=d3_clipWindow.y1)
  230.                if (*l <= *r)
  231.                  scanline (y, l, r, mask);
  232.                else
  233.                  scanline (y, r, l, mask);
  234.              y++;
  235.              increment (l, dl, mask);
  236.              increment (r, dr, mask);
  237.          }
  238.     }
  239. }
  240.  
  241.  
  242.  
  243.  
  244.  
  245. /*****************************************************************************
  246.   FUNCTION : d3_setClipWindow
  247.  
  248.   PURPOSE  : sets the clipping window for the polygon routines
  249.   RETURNS  : 
  250.   NOTES    :
  251.  
  252. ******************************************************************************/
  253.  
  254.  
  255.  
  256. void d3_setClipWindow (int x0, int y0, int x1, int y1)
  257. {
  258.     d3_clipWindow.x0 = x0;
  259.     d3_clipWindow.x1 = x1;
  260.     d3_clipWindow.y0 = y0;
  261.     d3_clipWindow.y1 = y1;
  262. }
  263.  
  264.  
  265.  
  266.  
  267. /*****************************************************************************
  268.   FUNCTION : d3_drawLine
  269.  
  270.   PURPOSE  : draw a line
  271.   RETURNS  : 
  272.   NOTES    :
  273.  
  274. ******************************************************************************/
  275.  
  276.  
  277.  
  278.  
  279. void d3_drawLine (int x1, int y1, int x2, int y2)
  280. {
  281.     XDrawLine(d3_display, d3_window, d3_gc, x1, y1, x2 ,y2);
  282. }
  283.  
  284.  
  285.  
  286.  
  287. /*****************************************************************************
  288.   FUNCTION : d3_intens_to_grayval
  289.  
  290.   PURPOSE  : converts the polygon light intensity in a palette gray value
  291.   RETURNS  : palette index
  292.   NOTES    :
  293.  
  294. ******************************************************************************/
  295.  
  296.  
  297.  
  298. int d3_intens_to_grayval (float intens)
  299. {
  300.     return (floor ((1.0 - intens) * (float) MAXCOLSTEPS));
  301. }
  302.  
  303.  
  304.  
  305.  
  306.  
  307. /*****************************************************************************
  308.   FUNCTION : d3_value_to_color
  309.  
  310.   PURPOSE  : converts the polygon light intensity in a palette color value
  311.   RETURNS  : palette index
  312.   NOTES    : 
  313.   UPDATE   : 3.3.93  shading for activation
  314.  
  315. ******************************************************************************/
  316.  
  317.  
  318. int d3_value_to_color (float value)
  319. {
  320.     return ((int) ((1.0 + value) * ((float) MAXCOLSTEPS - 3.0)));
  321. }
  322.  
  323.  
  324.  
  325.  
  326. /*****************************************************************************
  327.   FUNCTION : d3_setColor
  328.  
  329.   PURPOSE  : sets the drawing color
  330.   RETURNS  : 
  331.   NOTES    :
  332.  
  333. ******************************************************************************/
  334.  
  335.  
  336. void d3_setColor (long unsigned int color)
  337. {
  338.     XSetForeground(d3_display, d3_gc, 
  339.                    ui_col_rangePixels[color]); 
  340. }
  341.  
  342.  
  343.  
  344. /*****************************************************************************
  345.   FUNCTION : d3_setBlackColor
  346.  
  347.   PURPOSE  : sets the drawing color to black
  348.   RETURNS  : 
  349.   NOTES    :
  350.  
  351. ******************************************************************************/
  352.  
  353.  
  354.  
  355. void d3_setBlackColor (void)
  356. {
  357.     XSetForeground(d3_display, d3_gc, 
  358.                    BlackPixel (d3_display, d3_screen)); 
  359. }
  360.  
  361.  
  362.  
  363. /*****************************************************************************
  364.   FUNCTION : d3_setLinkColor
  365.  
  366.   PURPOSE  : calculates the link color from a value
  367.   RETURNS  : 
  368.   NOTES    :
  369.  
  370. ******************************************************************************/
  371.  
  372.  
  373.  
  374. void d3_setLinkColor (float *weight)
  375. {
  376.     int procent_value;
  377.     float quotient;
  378.  
  379.     if (*weight >= 0.0) {
  380.     if (d3_state.link_scale != 0.0)
  381.     {
  382.         quotient = fabs((*weight) / d3_state.link_scale);
  383.         if (quotient < 1.0)
  384.         procent_value = (int)(100.0 * quotient);
  385.         else
  386.         procent_value = 100;
  387.     }
  388.     else
  389.         procent_value = 100;
  390.         XSetForeground (d3_display, d3_gc, ui_col_rangePixels[MAXCOLSTEPS + 
  391.             procent_value * MAXCOLSTEPS / 100]);
  392.      } else {
  393.          if (d3_state.link_scale != 0.0)
  394.      {
  395.         quotient = fabs((*weight) / d3_state.link_scale);
  396.         if (quotient < 1.0)
  397.         procent_value = (int)(100.0 * quotient);
  398.         else
  399.         procent_value = 100;
  400.      }
  401.      else
  402.         procent_value = 100;
  403.      XSetForeground (d3_display, d3_gc, ui_col_rangePixels[MAXCOLSTEPS - 
  404.              procent_value * MAXCOLSTEPS / 100]);
  405.      }
  406. }        
  407.  
  408.  
  409.  
  410.  
  411. /*****************************************************************************
  412.   FUNCTION : d3_clearDisplay
  413.  
  414.   PURPOSE  : clears the display window
  415.   RETURNS  : 
  416.   NOTES    :
  417.  
  418. ******************************************************************************/
  419.  
  420.  
  421. void d3_clearDisplay (void)
  422. {
  423.     XClearWindow (d3_display, d3_window);
  424. }
  425.  
  426.  
  427.  
  428. /*****************************************************************************
  429.   FUNCTION : d3_getRootSizes
  430.  
  431.   PURPOSE  : gets the dimensions of the root window
  432.   RETURNS  : width and height
  433.   NOTES    :
  434.  
  435. ******************************************************************************/
  436.  
  437.  
  438. void d3_getRootSizes (unsigned int *xsize, unsigned int *ysize)
  439. {
  440.     Window root;
  441.     int x, y;
  442.     unsigned int width, height, border, depth;
  443.     Status status;
  444.  
  445.     status = XGetGeometry (ui_display, DefaultRootWindow (ui_display), &root, 
  446.                            &x, &y, &width, &height, &border, &depth);
  447.     *xsize = width;
  448.     *ysize = height;
  449. }
  450.  
  451.  
  452.  
  453.  
  454. /* end of file */
  455. /* lines: 512 */
  456.